//  ========== 
//  = 点点专享产品详情 业务JS = 
//  ========== 
var mmplanId;
var startinInterestConditionMap = {};//起息模型
startinInterestConditionMap[1] = '投标日起息';
startinInterestConditionMap[2]='投标日+1起息';
startinInterestConditionMap[3] = '满标日起息';
startinInterestConditionMap[4]='招标截止日起息';
startinInterestConditionMap[5]='满标日+1起息';
var Main = function() {
	if('false' === Util.requestURL('buyFlag')){
		$('.m-proj-footer').hide();
	}
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	this.index = 0; //公告滚动索引
	this.init();

};
Main.prototype = {
	init: function() {
		var _this = this;
		_this.tab();
		//加载数据
		_this.loadData();
		_this.eventInit()
	},
	tab: function() {
		var $win = $(window);
		var $scrollTo = $('#G-proj');
		var $slider = $('#slider');
		var $tab = $('#sliderSegmentedControl');
		var $tab_bar = $('#sliderProgressBar');
		$win.scroll(function() {
			var index = $tab.find('.mui-control-item.mui-active').index(); //当前显示tab索引
			if($win.scrollTop() >= ($scrollTo.height() - 1)) {
				$tab.addClass('nav-fixed');
				$tab_bar.addClass('nav-bar-fixed');
			} else {
				$tab.removeClass('nav-fixed');
				$tab_bar.removeClass('nav-bar-fixed');
			}
		})
	},
	eventInit: function() { //绑定事件
		$('#G-go-buy').on('tap', function() {
			if(!Util.checkLogin()){
    			mui.openWindow({
					url:'login.html'
				});
		    }else{
				if(!$(this).hasClass('m-btn-disabled')){
					mui.openWindow({
						url: 'bidSaleConfirm.html?mmplanId=' + mmplanId,
						id:'bidSaleConfirm'
					})
				}else{
					return false;
				}
			}
		});
        $('.m-btn-contract').on('tap',function(){//查询协议
			//var orderId = this.getAttribute('data-id');
			mui.openWindow('buyInfoUContract.html');
		});
	},

	//加载数据
	loadData: function() {
		var _this = this;
		mmplanId = Util.requestURL('mmplanId');
		var url = server_url + '/creditFlow/financingAgency/getDetailPlManageMoneyPlan.do';
		var param = {
			isMobile: 1,
			mmplanId: mmplanId
		};
		//加载数据
		$.getJSON(url, param, function(retData) {
			var data = retData.data;
			if(data.state == '1'){
				$("#G-go-buy").removeClass('m-btn-disabled');
			}
			$(".m-bid-pro-name").html(data.mmName); //产品名称
			if('1' == data.novice) { //新标记
				$(".m-proj-tag-div").html('<span class="mui-h5 m-proj-tag tag-ble">新</span>');
			}
			if('1' == data.coupon) { //券标记
				$(".m-proj-tag-div").html('<span class="mui-h5 m-proj-tag tag-yew">券</span>');
			}
			//年化收益
			$(".m-proj-lt .m-big").html(data.yeaRate);
			if(data.addRate && data.addRate > 0) {
				$(".m-proj-lt .m-small").html('+'+data.addRate);
			}
			//进度
			$('.z-act i').css('width', data.progress + '%');
			$('.z-act .m-range-round').css('left', data.progress + '%');
			//剩余期限
			$('.m-loan-life').html(data.investlimit+'天');
			//可投金额
			$('.m-after-money').html(data.afterMoney);
			//总额
			$('.m-bid-money').html(data.sumMoney);
			//起投金额
			$('.m-start-money').html(data.startMoney + '元起');
			//项目编号
			$('.m-bid-pro-number').html(data.mmNumber);
			//购买截止日期
			$('.m-bid-end-time').html(data.buyEndTime.substr(0,10));
			//投标名称
			$(".m-bid-pro-name").html(data.mmName);
			//招标说明
			$(".m-bid-remark").html(data.bidRemark);
			//投资范围
			$(".m-invest-scope").html(data.investScope);
			//收益方式
			if('0' == data.isOne) {
				$(".m-income-type").html('按期收息,到期还本');
			} else {
				$(".m-income-type").html('一次性支付利息');
			}
			//锁定期
			$(".m-locking-limit").html(data.lockingLimit);
			//投资起投金额
			$(".m-start-money").html(data.startMoney);
			//加入费率
			$(".m-join-rate").html(data.joinRate);
			//提前退出费率
			$(".m-earlier-out-rate").html(data.earlierOutRate);
			//起息模型
			$(".m-startin-interest-condition").html(startinInterestConditionMap[data.startinInterestCondition]);
			//购买开放时间
			$(".m-buy-start-time").html(data.buyStartTime);
			//购买截止时间
			$('.m-buy-end-time').html(data.buyEndTime.substr(0,10));
			//起息时间
			$(".m-startin-interest-time").html(data.startinInterestTime);
			//到期赎回方式
			$(".m-expire-redemption-way").html(data.expireRedemptionWay);
			//是否支持提前退出
			if(data.lockingLimit < data.investlimit) {
				$(".m-earlier-quit").html('支持');
			} else {
				$(".m-earlier-quit").html('不支持');
			}
			//保障方式
			$(".m-guarantee-way").html(data.guaranteeWay);
			//项目进程-发布时间
			$(".m-proc-buy-start-time").html(data.buyStartTime);
			//项目进程-计划金额
			$(".m-proc-sum-money").html(data.sumMoney);
			//			//安全保障
			//			if(data.listMaterials){
			//				for(k in data.listMaterials){
			//					$(".m-proj-list-materials").append('<li class="mui-table-view-cell">'+data.listMaterials[k].imgUrl+'</li>');
			//				}
			//			}
			//存储起投金额、递增金额、上限金额、可投金额
			//Util.setItem("startMoney",data.startMoney);//起投金额
			//Util.setItem("riseMoney",data.riseMoney);//递增金额
			//Util.setItem("limitMoney",Util.safeString(data.limitMoney));//上限金额
			//Util.setItem("afterMoney",data.afterMoney);//可投金额
		});
		_this._loadDataBuyInfo();
		_this._loadDataBuyInfoAndMoney();

	},
	_loadDataBuyInfo: function() {
		//加载项目项目进程
		var buyInfoUrl = server_url + '/creditFlow/financingAgency/getBuyInfoPlManageMoneyPlan.do';
		param = {
			isMobile: 1,
			mmplanId: mmplanId,
			page: 1,
			start: 0,
			limit: 1000
		}
		$.getJSON(buyInfoUrl, param, function(retData) {
			if(retData.success) {
				var htmlStr = '';
				for(var i = 0; i < retData.result.length; i++) {
					var data = retData.result[i];
					htmlStr += '<div class="list">';
					htmlStr += '<dl class="row">';
					htmlStr += '<dd>' + data.investPersonName + '</dd>';
					htmlStr += '<dd>' + data.buyMoney + '元</dd>';
					htmlStr += '<dd>' + data.buyDatetime + '</dd>';
					htmlStr += '</dl>'
					htmlStr += '</div>';
				}
				$("#buy-info-list").html(htmlStr);
			}
		});
	},
	_loadDataBuyInfoAndMoney: function() {
		//加载项目进程信息
		var buyInfoUrl = server_url + '/creditFlow/financingAgency/getBuyInfosizeandmoneyPlManageMoneyPlan.do';
		param = {
			isMobile: 1,
			mmplanId: mmplanId,
		}
		$.getJSON(buyInfoUrl, param, function(retData) {
			if(retData.success) {
				$('.m-proc-peo-num').html(retData.buyInfoList + '人');
				$('.m-curent-inte').html(retData.buyInfoListmoney + '元');
			}
		});
	}

}